package com.colortea.action.frontstage.membercenter;



import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

import com.colortea.action.backstage.base.DataCollectionTAction;
import com.colortea.action.backstage.utils.SHA1;
import com.colortea.action.backstage.utils.statickey.StaticKey;
import com.colortea.action.frontstage.utils.ImageCut;
import com.colortea.entity.MemberT;
import com.colortea.service.DeliverAddressTService;
import com.colortea.service.MemberTService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
 *	登陆相关的action
 * @author glory
 *	@data 2015年9月18日
 */
@ParentPackage("colortea")
@Namespace("")
@InterceptorRefs({  
    @InterceptorRef("defaultStack")
})
public class MemberAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1715464128768462374L;
	
	@Resource
	private DataCollectionTAction dataCollectionTAction;
	@Resource
	private MemberTService memberTService;
	@Resource
	private DeliverAddressTService deliverAddressTService;
	private String basePath;
	private String nikiname;
	private boolean sucflag;
	private boolean slogin;
	private String newpasssword;
	private String oldpassword;
	//选区x坐标
    private int x;
    //选区y坐标
    private int y;
    //选区宽度
    private int w;
    //选区高度
    private int h ;
    //界面图宽度/选区宽度
    private double rx;
    //界面图高度/选区高度
    private double ry;
    //界面显示图片的宽
    private int viewW;
    //界面显示图片的高
    private int viewH;
    //图片的路径
    private String imagepath;
	
	
	public String getBasePath() {
		return basePath;
	}
	public void setBasePath(String basePath) {
		this.basePath = basePath;
	}
	public String getNikiname() {
		return nikiname;
	}

	public void setNikiname(String nikiname) {
		this.nikiname = nikiname;
	}
	public boolean isSucflag() {
		return sucflag;
	}
	public void setSucflag(boolean sucflag) {
		this.sucflag = sucflag;
	}	
	public boolean isSlogin() {
		return slogin;
	}
	public void setSlogin(boolean slogin) {
		this.slogin = slogin;
	}	
	public String getNewpasssword() {
		return newpasssword;
	}
	public void setNewpasssword(String newpasssword) {
		this.newpasssword = newpasssword;
	}
	
	public String getOldpassword() {
		return oldpassword;
	}
	public void setOldpassword(String oldpassword) {
		this.oldpassword = oldpassword;
	}	
	public int getX() {
		return x;
	}
	public void setX(int x) {
		this.x = x;
	}
	public int getY() {
		return y;
	}
	public void setY(int y) {
		this.y = y;
	}
	public int getW() {
		return w;
	}
	public void setW(int w) {
		this.w = w;
	}
	public int getH() {
		return h;
	}
	public void setH(int h) {
		this.h = h;
	}
	public double getRx() {
		return rx;
	}
	public void setRx(double rx) {
		this.rx = rx;
	}
	public double getRy() {
		return ry;
	}
	public void setRy(double ry) {
		this.ry = ry;
	}
	public int getViewW() {
		return viewW;
	}
	public void setViewW(int viewW) {
		this.viewW = viewW;
	}
	public int getViewH() {
		return viewH;
	}
	public void setViewH(int viewH) {
		this.viewH = viewH;
	}	
	public String getImagepath() {
		return imagepath;
	}
	public void setImagepath(String imagepath) {
		this.imagepath = imagepath;
	}
	/**
	 * 清理错误
	 */
	@Override
	public void validate() {
		this.clearErrorsAndMessages();

	}	
	/**
	 * 进入登陆界面
	 * @return
	 */
	@Action(value = "gomemberinfo", results = { 
			@Result(name = "success",type="freemarker",location = "/WEB-INF/theme/default/shop/memberinfo.ftl"),
			@Result(name = "gologin",type="freemarker",location = "/WEB-INF/theme/default/shop/login.ftl"),
	})
	public String gomemberinfo(){
		dataCollectionTAction.putBaseInfoToContext();
		MemberT memberT=(MemberT) ActionContext.getContext().getSession().get(StaticKey.MEMBER_SESSION_KEY);
		if(memberT!=null){
			return SUCCESS;
		}
		return "gologin";
		
		
	}
	
	/**
	 * 修改昵称
	 * @return
	 */
	@Action(value = "updateNiki",results={@Result(name="json",type="json")})
	public String updateNiki(){
		dataCollectionTAction.putBaseInfoToContext();
		MemberT memberT=(MemberT) ActionContext.getContext().getSession().get(StaticKey.MEMBER_SESSION_KEY);
		if(memberT!=null){
			if (StringUtils.isNotBlank(this.getNikiname())){ 
				memberT.setNick(this.getNikiname());
				memberTService.update(memberT);
				this.setSucflag(true);
				this.setSlogin(false);
				return "json";
			}
			return "json";
		}
		this.setSlogin(false);
		return "json";		
	}
	
	/**
	 * 修改密码
	 */
	@Action(value = "updatePassword",results={@Result(name="json",type="json")})
	public String updatePassword(){
		dataCollectionTAction.putBaseInfoToContext();
		MemberT memberT=(MemberT) ActionContext.getContext().getSession().get(StaticKey.MEMBER_SESSION_KEY);
		if(memberT != null){
			this.setSlogin(true);
			if(StringUtils.isNotBlank(this.getNewpasssword().trim()) && StringUtils.isNotBlank(this.getOldpassword().trim())){
				String opwd = SHA1.getDigestOfString(this.getOldpassword().trim());
				if(StringUtils.equals(memberT.getLoginpwd(),opwd)){
					memberT.setLoginpwd(SHA1.getDigestOfString(this.getNewpasssword().trim()));
					memberTService.update(memberT);
					ActionContext.getContext().getSession().remove(StaticKey.MEMBER_SESSION_KEY);
					this.setSucflag(true);
					return "json";
				}else{
					this.setSucflag(false);
					return "json";
				}
			}
		}
		this.setSlogin(false);
		return "json";
	}
	
	/**
	 *修改头像图片大小
	 */
	@Action(value = "updateHeadPic",results={@Result(name="json",type="json")})
	public String updateHeadPic(){
		dataCollectionTAction.putBaseInfoToContext();
		MemberT memberT=(MemberT) ActionContext.getContext().getSession().get(StaticKey.MEMBER_SESSION_KEY);
		if(memberT != null){
			this.setSlogin(true);
			memberT.setHeadpath(this.getImagepath());
			String imagepath = ServletActionContext.getServletContext().getRealPath("/")+this.getImagepath();
			ImageCut.cutImage(imagepath,this.getX(), this.getY(), this.getW(),this.getH(), this.getViewW(),this.getViewH());
			memberTService.update(memberT);
			this.setSucflag(true);
			return "json";			
		}else{
			this.setSlogin(false);
			this.setSucflag(false);
			return "json";
		}
	}
	


	
	

	

	
	
}
